<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>定时任务管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息(<b>收起-展开</b>)</legend>
            <div id="search-div" style="margin: 10px 10px 10px 10px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="jobName" name="jobName" autocomplete="off"
                                       class="layui-input" placeholder="请输入任务名称">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">任务组名：</label>
                            <div class="layui-input-inline">
                                <select name="jobGroup" id="jobGroup" lay-filter="jobGroup">
                                    <option value="">所有</option>
                                    <option th:each="item: ${sysJobGroupDict}" th:text="${item.name}"
                                            th:value="${item.code}"></option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">任务状态：</label>
                            <div class="layui-input-inline">
                                <select name="status" id="status" lay-filter="status">
                                    <option value="">所有</option>
                                    <option th:each="item: ${sysJobStatusDict}" th:text="${item.name}"
                                            th:value="${item.code}"></option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-reset-btn">
                                <i class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新增
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">
                <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
            </a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="executeonce">
                <i class="layui-icon layui-icon-engine" style="font-size: 10px;"></i>执行一次
            </a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="record">
                <i class="layui-icon layui-icon-log" style="font-size: 10px;"></i>调度日志
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </a>
        </script>

        <script type="text/html" id="switchTpl">
            <input type="checkbox" name="quartzStatus" value="{{d.jobId}}" lay-skin="switch" lay-text="启动|停止"
                   lay-filter="quartzStatusDemo" {{ d.status== 0 ? 'checked' : '' }}>
        </script>


    </div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table;

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'job/list',
            method: 'post', //如果无需自定义HTTP类型，可不加该参数
            where: {}, // 初始化时带的参数
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {
                        field: '', width: '5%', title: '序号', type: 'numbers', templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1;
                        }
                    },
                    {field: 'jobId', width: '13%', title: '任务ID'},
                    {field: 'jobName', width: '10%', title: '任务名'},
                    {field: 'jobGroup', width: '10%', title: '任务组名'},
                    {field: 'invokeTarget', width: '15%', title: '调用目标'},
                    {field: 'cronExpression', width: '12%', title: 'cron表达式'},
                    {field: 'status', width: '10%', title: '任务状态', templet: '#switchTpl'},
                    {title: '操作', width: '28%', toolbar: '#currentTableBar', align: "center", fixed: 'right'}
                ]
            ],
            autoSort: false, //禁用前端自动排序。注意：该参数为 layui 2.4.4 新增
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line', // 行边框风格
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            }

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            currTable.reload({
                page: {
                    curr: 1 // 页码从1开始
                },
                where: {
                    jobName: data.field.jobName,
                    jobGroup: data.field.jobGroup,
                    status: data.field.status
                }
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            form.val("search-form", {
                "jobName": '',
                "jobGroup": '',
                "status": '',
            });
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1
                },
                where: form.val("search-form")
            });
            return false;
        });


        // 监听启用/停用操作
        form.on('switch(quartzStatusDemo)', function (obj) {
            console.log(this.value + ' ：' + this.name + '：' + obj.elem.checked);
            if (obj.elem.checked == true) {
                AjaxUtil.get({
                    url: AjaxUtil.ctx + "job/start/" + this.value,
                    success: function (res) {
                        if (res.code === 0) {
                            Message.success(1500, res.message, function () {
                                currTable.reload();
                            });
                        } else {
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            } else {
                AjaxUtil.get({
                    url: AjaxUtil.ctx + "job/stop/" + this.value,
                    success: function (res) {
                        if (res.code === 0) {
                            Message.success(1500, res.message, function () {
                                currTable.reload();
                            });
                        } else {
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            }
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加定时任务',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'job/forAdd',
                    end: function (index) {
                        // 重载表格
                        currTable.reload();
                    }
                });
            }
        });


        // 监听行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data);
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑定时任务',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'job/forUpdate/' + data.jobId,
                    end: function (index) {
                        // 重载表格
                        currTable.reload();
                    }
                });
                return false;
            } else if (obj.event === 'record') {
                var index = layer.open({
                    title: '定时任务执行日志',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'joblog/init/' + data.jobId,
                });
                return false;
            } else if (obj.event === 'delete') {

                layer.confirm('确定删除此定时任务吗？', function (index) {
                    var loading = layer.load(0, {shade: 0.1});
                    AjaxUtil.get({
                        url: AjaxUtil.ctx + "job/delete/" + data.jobId,
                        success: function (res) {
                            if (res.code === 0) {
                                layer.close(index);
                                layer.close(loading);
                                Message.success(1500, res.message, function () {
                                    // 重载表格
                                    currTable.reload();
                                });
                            } else {
                                layer.close(loading);
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
                return false;
            } else if (obj.event === 'executeonce') {
                var loading = layer.load(0, {shade: 0.1});
                AjaxUtil.get({
                    url: AjaxUtil.ctx + "job/executeonce/" + data.jobId,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.close(loading);
                            Message.success(1500, res.message, function () {
                                // 重载表格
                                currTable.reload();
                            });
                        } else {
                            layer.close(loading);
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            }
        });

    });

</script>

</body>
</html>